﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUAddress
    {
        /// <summary>
        /// Get All the Address of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<Address> GetAllAddress(bool searchInDeleted)
        {
            List<Address> adds = new List<Address>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from Address in db.Address
                              where Address.add_isdeleted == searchInDeleted
                              select Address;
                foreach (Address tmp in results)
                    adds.Add(tmp);
            }

            return adds;
        }

        /// <summary>
        /// Get the Address by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Address GetAddressByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var add = from Address in db.Address
                              where Address.add_id == id
                              select Address;
                    foreach(Address tmp in add)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// Add a Address into the database.
        /// </summary>
        /// <param name="add"></param>
        /// <returns></returns>
        public static Address AddAddress(Address add)
        {
            if (add != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    Address nadd = db.Address.Add(add);
                    db.SaveChanges();
                    return nadd;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific Address of the database.
        /// </summary>
        /// <param name="add"></param>
        /// <returns></returns>
        public static bool RemoveAddress(Address add)
        {
            if (add != null)
            {
                add.add_isdeleted = true;
                if (UpdateAddress(add) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the Address into the database.
        /// </summary>
        /// <param name="add"></param>
        /// <returns></returns>
        public static Address UpdateAddress(Address add)
        {
            if (add != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from Address in db.Address
                                  where Address.add_id == add.add_id
                                  select Address;

                    foreach (var tmp in current)
                    {
                        tmp.add_address = add.add_address;
                        tmp.add_city = add.add_city;
                        tmp.add_gpsx = add.add_gpsx;
                        tmp.add_gpsy = add.add_gpsy;
                        tmp.add_label = add.add_label;
                        tmp.add_zipcode = add.add_zipcode;
                        tmp.cou_id = add.cou_id;
                        tmp.add_isdeleted = add.add_isdeleted;

                        db.SaveChanges();
                    }

                    add = current as Address;
                }
            }

            return add;
        }
    }
}
